package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysInformation;
import com.ruoyi.system.service.ISysInformationService;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.Put;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ImportantReminderMapper;
import com.ruoyi.system.domain.ImportantReminder;
import com.ruoyi.system.service.IImportantReminderService;

/**
 * 要事提醒
Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-18
 */
@Service

public class ImportantReminderServiceImpl implements IImportantReminderService 
{
    @Autowired
    private ImportantReminderMapper importantReminderMapper;
    @Autowired
    private ISysInformationService sysInformationService;

    /**
     * 查询要事提醒

     *
主键
     * @return 要事提醒

     */
    @Override
    public List<ImportantReminder> selectImportantReminderById()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysInformation> sysInformations = sysInformationService.selectProjectorList(userId);
        List<ImportantReminder> list = new ArrayList<>();
        if(null!=sysInformations) {
            for (SysInformation information : sysInformations) {
                if(null != information){
                    ImportantReminder importantReminder = importantReminderMapper.selectImportantReminderById(information.getId());
                    if(null!=importantReminder){
                        list.add(importantReminder);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public ImportantReminder selectImportantReminderByprojectId(Long projectId) {
        return importantReminderMapper.selectImportantReminderByprojectId(projectId);
    }

    /**
     * 查询要事提醒
列表
     * 
     * @param importantReminder 要事提醒

     * @return 要事提醒

     */
    @Override
    public List<ImportantReminder> selectImportantReminderList(ImportantReminder importantReminder)
    {
        return importantReminderMapper.selectImportantReminderList(importantReminder);
    }

    /**
     * 新增要事提醒

     *

     * @return 结果
     */
    @Override
    public int insertImportantReminder()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysInformation> sysInformations = sysInformationService.selectProjectorList(userId);
        if(sysInformations!=null) {
            for (SysInformation sysInformation : sysInformations) {
                ImportantReminder importantReminder2 = importantReminderMapper.selectImportantReminderByprojectId(sysInformation.getId());
                if(null == importantReminder2 && sysInformation!=null) {
                    ImportantReminder importantReminder = new ImportantReminder();
                    importantReminder.setProjectId(sysInformation.getId());
                    importantReminder.setApprovalTime(sysInformation.getReadTime());
                    Date startTime = sysInformation.getStartTime();
                    Date endTime = sysInformation.getEndTime();
                    importantReminder.setProjectName(sysInformation.getItemName());
                    importantReminder.setStartTime(startTime);
                    importantReminder.setEndTime(endTime);
                    Date date = new Date();
                    int days = (int) ((date.getTime() - startTime.getTime()) / (1000 * 3600 * 24));
                    importantReminder.setContinuedTime(days);
                    importantReminderMapper.insertImportantReminder(importantReminder);
                }
                if(null != importantReminder2){
                    ImportantReminder importantReminder = new ImportantReminder();
                    importantReminder.setProjectId(sysInformation.getId());
                    importantReminder.setApprovalTime(sysInformation.getReadTime());
                    Date startTime = sysInformation.getStartTime();
                    Date endTime = sysInformation.getEndTime();
                    importantReminder.setProjectName(sysInformation.getItemName());
                    importantReminder.setStartTime(startTime);
                    importantReminder.setEndTime(endTime);
                    Date date = new Date();
                    int days = (int) ((date.getTime() - startTime.getTime()) / (1000 * 3600 * 24));
                    importantReminder.setContinuedTime(days);
                    importantReminderMapper.updateImportantReminder(importantReminder);
                }
            }
        }
        return 1;
    }

    /**
     * 修改要事提醒

     * 
     * @param importantReminder 要事提醒

     * @return 结果
     */
    @Override
    public int updateImportantReminder(ImportantReminder importantReminder)
    {
        return importantReminderMapper.updateImportantReminder(importantReminder);
    }

    /**
     * 批量删除要事提醒

     * 
     * @param ids 需要删除的要事提醒
主键
     * @return 结果
     */
    @Override
    public int deleteImportantReminderByIds(Long[] ids)
    {
        return importantReminderMapper.deleteImportantReminderByIds(ids);
    }

    /**
     * 删除要事提醒
信息
     * 
     * @param id 要事提醒
主键
     * @return 结果
     */
    @Override
    public int deleteImportantReminderById(Long id)
    {
        return importantReminderMapper.deleteImportantReminderById(id);
    }

}
